package com.atguigu.srb.core.controller.admin;


import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.utils.PageUtils;
import com.atguigu.srb.common.utils.R;
import com.atguigu.srb.common.utils.ResponseEnum;
import com.atguigu.srb.core.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;

/**
 * <p>
 * 积分等级表 前端控制器
 * </p>
 *
 * @author cfx
 * @since 2022-06-25
 */
@Slf4j
@Api(tags = "积分登记管理")
//@CrossOrigin   //跨域
@RestController
@RequestMapping("/admin/core/integralGrade")
public class AdminIntegralGradeController {

    /**
     * 查询积分列表
     */
    @Autowired
    private IntegralGradeService integralGradeService;

    @ApiOperation("积分等级列表")
    @GetMapping("/list")
    public R listAll(@RequestParam Map<String, Object> params) {
        PageUtils page  = integralGradeService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 根据id删除积分等级（逻辑删除）
     */
    @ApiOperation(value = "根据id删除积分等级", notes = "逻辑删除")
    @DeleteMapping("/remove")
    public R removeById(@RequestBody Long[] Ids) {
        boolean result = integralGradeService.removeByIds(Arrays.asList(Ids));
        if (result) {
            return R.ok();
        } else {
            return R.error(-1, "删除失败");
        }
    }

    /**
     * 新增积分等级
     *
     * @param integralGrade
     * @return
     */
    @ApiOperation("新增积分等级")
    @PostMapping("/save")
    public R save(@RequestBody IntegralGrade integralGrade) {
      /*  if (integralGrade.getBorrowAmount() == null) {
            throw new BusinessException(ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        }*/
        Assert.notNull(integralGrade.getBorrowAmount(),ResponseEnum.BORROW_AMOUNT_NULL_ERROR);
        boolean result = integralGradeService.save(integralGrade);
        if (result) {
            return R.ok();
        } else {
            return R.error(-1, "保存失败");
        }
    }

    /**
     * 根据id获取积分等级
     *
     * @param id
     * @return
     */
    @ApiOperation("根据id获取积分等级")
    @GetMapping("/info/{id}")
    public R getById(@PathVariable Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        if (integralGrade != null) {
            return R.ok().put("record", integralGrade);
        } else {
            return R.error(-1, "数据不存在");
        }
    }

    /**
     * 更新积分等级
     *
     * @param integralGrade
     * @return
     */
    @ApiOperation("更新积分等级")
    @PutMapping("/update")
    public R updateById(@RequestBody IntegralGrade integralGrade) {
        boolean result = integralGradeService.updateById(integralGrade);
        if (result) {
            return R.ok();
        } else {
            return R.error(-1, "修改失败");
        }
    }

}

